<template>
  <el-dialog
    v-model="props.failureDialog"
    :title="props.failureTitle"
    :show-close="false"
    width="35%"
  >
    <el-form :model="form" :rules="rules" ref="ruleFormRef">
      <el-form-item label="设备类型" prop="deviceTypeName">
        <div style="display: flex; justify-content: space-between; width: 100%">
          <el-input
            v-model="form.deviceTypeName"
            placeholder="请选择"
            class="input-with-select"
            style="width: 75%"
            @focus="selectType('type2')"
          >
          </el-input>
          <el-button
            @click="selectType('type2')"
            style="width: 20%"
            type="primary"
            plain
            >选择</el-button
          >
        </div>
      </el-form-item>
      <el-form-item label="故障类型" prop="failureName">
        <el-input
          v-model="form.failureName"
          autocomplete="off"
          placeholder="请输入故障类型"
          type="textarea"
          row="4"
          show-word-limit
          maxlength="128"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel(ruleFormRef)">取消</el-button>
        <el-button
          type="primary"
          :loading="loading"
          @click="submit(ruleFormRef)"
        >
          确定
        </el-button>
      </span>
    </template>
    <EquipmentTypeDialog
      :typeDialog="typeDialog"
      :typeForm="typeForm"
      @toBack="toBack"
      @deviceTypeSub="deviceTypeSub"
    >
    </EquipmentTypeDialog>
  </el-dialog>
</template>
<script setup lang="ts">
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import {
  ref,
  reactive,
  getCurrentInstance,
  defineAsyncComponent,
  watch
} from 'vue'
import { addFailureTermApi, editFailureTermApi } from '/@/api/repair'
const EquipmentTypeDialog = defineAsyncComponent(
  () => import('../../assets/components/EquipmentTypeDialog.vue')
)
const { proxy } = getCurrentInstance() as any
const props = defineProps({
  failureDialog: Boolean,
  failureForm: Object,
  failureTitle: String
})
const emit = defineEmits(['failureCancel', 'failureSubmit'])
const ruleFormRef = ref<FormInstance>()
const formLabelWidth = ref('100px')
const loading = ref(false)
const form = ref({
  parentId: '0',
  deviceType: '',
  deviceTypeName: '',
  failureName: '',
  solution: ''
})

const rules = reactive<FormRules>({
  failureName: [{ required: true, message: '请输入故障类型', trigger: 'blur' }],
  deviceTypeName: [
    { required: true, message: '请选择设备类型', trigger: 'change' }
  ]
})
const typeDialog = ref<boolean>(false)
const typeForm = {
  width: '25%',
  title: '',
  voucher: ''
}

const selectType = (str: string) => {
  typeForm.voucher = str
  typeForm.title = '设备类型选择'
  typeDialog.value = !typeDialog.value
}

const toBack = () => {
  typeDialog.value = !typeDialog.value
}

const deviceTypeSub = (val: any) => {
  form.value.deviceType = val.deviceTypeId
  form.value.deviceTypeName = val.deviceTypeName
  typeDialog.value = !typeDialog.value
}

const cancel = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emit('failureCancel')
}

const submit = (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      if (props.failureTitle == '新增') {
        addFailureTermApi(form.value).then((res) => {
          if (res.code == 0) {
            ElMessage.success('新增成功')
            loading.value = false
            emit('failureSubmit')
          } else {
            ElMessage.success('新增失败')
            loading.value = false
          }
        })
      } else {
        Reflect.set(form.value, 'id', props.failureForm?.id)
        editFailureTermApi(form.value).then((res) => {
          if (res.code == 0) {
            ElMessage.success('编辑成功')
            loading.value = false
            emit('failureSubmit')
          } else {
            ElMessage.success('编辑失败')
            loading.value = false
          }
        })
      }
    } else {
      loading.value = false
    }
  })
}

watch(
  () => props.failureDialog,
  () => {
    if (props.failureTitle == '编辑') {
      form.value.deviceTypeName = props.failureForm?.deviceTypeName
      form.value.deviceType = props.failureForm?.deviceType
      form.value.failureName = props.failureForm?.failureName
    } else {
      form.value.deviceTypeName = ''
      form.value.failureName = ''
    }
  }
)
</script>
<style scoped lang="scss"></style>
